package com.duqc.freemarker;

import java.util.Optional;

/**
 * @ClassName MysqlType
 * @Description mysql数据类型
 * @Author duqc
 * @Date 2021/3/1
 **/
public enum  MysqlType {

    FLOAT("FLOAT", "Double", ""),
    DOUBLE("DOUBLE", "Double", ""),

    CHAR("CHAR", "String", ""),
    VARCHAR("VARCHAR", "String", ""),
    LONGTEXT("LONGTEXT", "String", ""),
    BLOB("BLOB", "byte[]", ""),
    TEXT("TEXT", "String", ""),
    MEDIUMTEXT("MEDIUMTEXT", "String", ""),

    INTEGER("INTEGER", "Long", ""),
    INT("INT", "Integer", ""),
    SMALLINT("SMALLINT", "Integer", ""),
    MEDIUMINT("MEDIUMINT", "Integer", ""),
    TINYINT("TINYINT", "Integer", ""),
    DECIMAL("DECIMAL", "BigDecimal", "java.math.BigDecimal"),
    BIGINT("BIGINT", "Long", ""),

    DATE("DATE", "String", ""),
    TIME("TIME", "Time", "java.sql.Time"),
    DATETIME("DATETIME", "String", ""),
    TIMESTAMP("TIMESTAMP", "Date", "java.util.Date"),
    YEAR("YEAR", "Date", "java.util.Date"),
    ;

    /**
     * mysql数据类型
     */
    private final String typeName;

    /**
     * Java数据类型
     */
    private final String dataTypeName;

    /**
     * Java包名
     */
    private final String packageName;

    MysqlType(String typeName, String dataTypeName, String packageName) {
        this.typeName = typeName;
        this.dataTypeName = dataTypeName;
        this.packageName = packageName;
    }

    private String getTypeName() {
        return typeName;
    }

    private String getDataTypeName() {
        return dataTypeName;
    }

    private String getPackageName() {
        return packageName;
    }

    /**
     * 通过mysql数据类型查询对应的Java数据类型
     *
     * @param typeName mysql数据类型
     * @return String
     */
    public static String getDataTypeNameByTypeName(String typeName) {
        return Optional.ofNullable(typeName).map(name -> {
            MysqlType[] mysqlTypes = MysqlType.values();
            for (MysqlType mysqlType : mysqlTypes) {
                if (typeName.equals(mysqlType.getTypeName())) {
                    name = mysqlType.getDataTypeName();
                    break;
                }
            }
            return name;
        }).orElse(null);
    }

    /**
     * 通过mysql数据类型查询对应的Java包名
     *
     * @param typeName mysql数据类型
     * @return String
     */
    public static String getPackageNameByTypeName(String typeName) {
        return Optional.ofNullable(typeName).map(name -> {
            MysqlType[] mysqlTypes = MysqlType.values();
            for (MysqlType mysqlType : mysqlTypes) {
                if (typeName.equals(mysqlType.getTypeName())) {
                    name = mysqlType.getPackageName();
                    break;
                }
            }
            return name;
        }).orElse(null);
    }
}
